package ldkxingzhe.l365calendar.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import ldkxingzhe.l365calendar.R;
import ldkxingzhe.l365calendar.fragment.MainDiscovery;
import ldkxingzhe.l365calendar.fragment.MainList;
import ldkxingzhe.l365calendar.fragment.MainMore;

/**
 * 主界面
 * Created by ke on 16-1-16.
 */
public class MainTab extends BaseActivity{
    @SuppressWarnings("unused")
    private static final String TAG = "MainTab";

    FrameLayout fragmentContent;
    final Class[] fragmentClass = {MainList.class, MainMore.class, MainDiscovery.class, MainMore.class};
    View[] tabs = new View[fragmentClass.length];
    FragmentManager fragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_tab_layout);
        fragmentContent = findView(R.id.content);
        initTabs();
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        onTabChanged(0, true);
        tabs[0].setSelected(true);
        resetTransaction();
    }

    private void initTabs() {
        tabs[0] = findView(R.id.tab_one);
        tabs[1] = findView(R.id.tab_two);
        tabs[2] = findView(R.id.tab_three);
        tabs[3] = findView(R.id.tab_four);

        for(View view : tabs){
            view.setOnClickListener(tabOnClick);
        }
    }
    private FragmentTransaction transaction = null;
    // position --- fragment 的位置, isSelect --- fragment被选中
    private void onTabChanged(int position, boolean isSelect) {
        Log.v(TAG, "onTabChanged, and position " + position + ", isSelect is " + isSelect);
        if(transaction == null){
            throw new IllegalStateException("transaction is null, please make 'transaction' meaningful");
        }
        Fragment fragment = fragmentManager.findFragmentByTag(fragmentClass[position].getSimpleName());
        if(fragment == null && isSelect){
            try {
                fragment = (Fragment) fragmentClass[position].newInstance();
                transaction.add(fragmentContent.getId(), fragment, fragmentClass[position].getSimpleName());
            } catch (Exception e) {
                Log.e(TAG, e.getMessage(), e);
                return;
            }
        }else if(fragment == null){
            return;
        }

        if(isSelect){
            transaction.show(fragment);
        }else{
            transaction.hide(fragment);
        }
    }

    private View.OnClickListener tabOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            transaction = fragmentManager.beginTransaction();
            for(int i = 0; i < tabs.length; i++){
                View view = tabs[i];
                boolean isSelect = false;
                if(v == view){
                    isSelect = true;
                }
                view.setSelected(isSelect);
                onTabChanged(i, isSelect);
            }
            resetTransaction();
        }
    };

    private void resetTransaction() {
        transaction.commit();
        transaction = null;
    }
}
